package uf.audit.demo.biz;

import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;

import uf.audit.tax.platform.core.spring.bean.User;
import uf.audit.tax.platform.util.Consts;
import uf.audit.tax.platform.util.Utils;
import uf.audit.tax.platform.util.sso.UserInfo;
import uf.audit.tax.platform.web.UFBaseController;

public class UFBiz {

	public static void setAJID(Controller controller, String ajid) {
		HttpSession session = controller.getSession(true);
		session.setAttribute(Consts.SESSION_TOKEN_AJID, ajid);
	}

	public static String getAJID(Controller controller) {
		Object value = controller.getSession(true).getAttribute(Consts.SESSION_TOKEN_AJID);
		if (value != null)
			return (String) value;
		return null;
	}

	public static UserInfo buildUserInfo(String user_id) {
		UserInfo user_info = new UserInfo();
		user_info.setUserBh(user_id);
		ApplicationContext context = Utils.getApplicationContext();
		User user = context.getBean("User", User.class);
		Record rec = user.getUserInfo(user_info.getUserBh());
		if (rec == null)
			return null;
		String orgbh = rec.getStr("ORGBH");
		if (orgbh == null)
			orgbh = "0";
		user_info.setOrgBhs(orgbh);
		user_info.setIsLeader(0);
		if (rec.getBigDecimal("ISSUPER") != null)
			user_info.setIsLeader(rec.getBigDecimal("ISSUPER").intValue());
		user_info.setRealName(rec.getStr("REALNAME"));
		user_info.setUserName(rec.getStr("USERNAME"));
		user_info.setOrgNames(rec.getStr("ORGNAME"));
		user_info.setRoleNames(rec.getStr("ROLENAME"));
		if (user_info.getIsLeader() == 1 && !orgbh.equals("0") && !user_info.getUserName().equals("admin")) {
			user_info.setOrgBhs(user.getUserOrgs(orgbh));
		}
		return user_info;
	}

	public static void saveUserInfo(UFBaseController con, UserInfo info) {
		con.getSession(true).setAttribute("tax_platform_user_info", info);
	}

	public static UserInfo getUserInfo(UFBaseController con) {
		Object value = con.getSession(true).getAttribute("tax_platform_user_info");
		if (value == null)
			return null;
		return (UserInfo) value;
	}

}
